<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .cmm {
      height: 100px;
      width: 100px;
      background-color: red;
      transform: rotateX('angle');
}
  </style>
</head>

<body>
  <input type="text">
  <button id="my-button">执行任务</button>
  <div class="cmm"></div>
  <!-- box容器 -->
  <div id="box" style="height:500px;width:400px;overflow: auto;margin-top:24px;">
</body>
<script>
  const box = document.getElementById('box')
  for (let i = 0; i < 1000; i++) {
    const myText = document.createElement('h2')
    myText.innerText = i
    box.appendChild(myText)
  }
  const myButton = document.getElementById('my-button')
  myButton.addEventListener('click', load)
  // function load() {
  //   const total = 300000;
  //   for (let i = 0; i < total; i++) {
  //     console.log(i)
  //   }
  // }
  // 分片-setTimeout
  // function load() {
  //   const total = 3000000
  //   const length = 2000
  //   function loop(curTotal, curIndex) {
  //     if(curTotal <= 0) return
  //     let pageCount = Math.min(curTotal, length)
  //     let curEnd = curIndex + pageCount
  //     setTimeout(() => {
  //       for(let i = curIndex; i < curEnd; i++) {
  //         console.log(i, 12);
  //       }
  //       loop(curTotal - pageCount, curIndex + pageCount)
  //     }, 0)
  //   }
  //   loop(total, 0)
  // }
  // 分片- requestAnimationFrame
  function load() {
    const total = 300000
    const length = 1000
    function loop(curTotal, curIndex) {
      if(curTotal <= 0) return
      let pageCount = Math.min(curTotal, length)
      let curEnd = curIndex + pageCount
      for(let i = curIndex; i < curEnd; i++) {
        console.log(i);
      }
      requestAnimationFrame((s) => loop(curTotal - pageCount, curIndex + pageCount))
    }
    requestAnimationFrame((s) => loop(total, 0))
  }
</script>

</html>